import _ from 'lodash'
import {calcAnnualizedByTerminationPaymentDate} from "@/utils/util";
import { isNotVoid } from '../../../utils/util';

// 处理不同期权的希腊字母
export function calcGreek(otcOptionJson, result) {
    let productType = otcOptionJson.calcInstrumentForm.productType
    
    switch (productType){
        case "Vanilla":
            /*
                *
                * 希腊字母单位化的情况下，香草需要对计算结果乘上期权份数
                *
                * 以下为需要处理的希腊字母
                * ['rho', 'theta', 'delta', 'deltaForward', 'vega', 'gamma', 'gammaForward', 'vanna', 'volga', 'dvbp', 'gammacash', 'deltacash']
                *
                * */
            let vanillaOptionEntitlement =  result.contract.optionEntitlement
            let vanillaArr = ['rho', 'theta', 'delta', 'deltaForward', 'vega', 'gamma', 'gammaForward', 'vanna', 'volga', 'dvbp', 'gammacash', 'deltacash']
            let numberOfOption = otcOptionJson.calcInstrumentForm.moneynessOption == '1' ? calcAnnualizedByTerminationPaymentDate(otcOptionJson.calcInstrumentForm) : '1'
            vanillaArr.forEach(item=>{
                result.contract[item] = isNotVoid(result.contract[item]) ? result.contract[item] : 0;
                result.contract[item] = _.multiply(_.multiply(result.contract[item] , vanillaOptionEntitlement), numberOfOption)
            })
          break;
        case "Digital":
            /*
                *
                * 希腊字母单位化的情况下，二元需要对计算结果乘上收益金额
                *
                * 以下为需要处理的希腊字母
                * ['rho', 'theta', 'delta', 'deltaForward', 'vega', 'gamma', 'gammaForward', 'vanna', 'volga', 'dvbp', 'gammacash', 'deltacash']
                *
                * */
            let digitalPaymentAmount = otcOptionJson.calcInstrumentForm.paymentAmount
            let digitalArr = ['rho', 'theta', 'delta', 'deltaForward', 'vega', 'gamma', 'gammaForward', 'vanna', 'volga', 'dvbp', 'gammacash', 'deltacash']
            digitalArr.forEach(item=>{
                result.contract[item] = isNotVoid(result.contract[item]) ? result.contract[item] : 0;
                result.contract[item] = _.multiply(result.contract[item] , digitalPaymentAmount)
            })
          break;
        // case "Barrier":
        //   resultJson = buildXirJsonBarrier(tradeForm,resultJson);
        //   break;
        default:
            /*
                *
                * 希腊字母单位化的情况下，默认对计算结果乘上期权份数
                *
                * 以下为需要处理的希腊字母
                * ['rho', 'theta', 'delta', 'deltaForward', 'vega', 'gamma', 'gammaForward', 'vanna', 'volga', 'dvbp', 'gammacash', 'deltacash']
                *
                * */
            let optionEntitlement =  result.contract.optionEntitlement
            let arr = ['rho', 'theta', 'delta', 'deltaForward', 'vega', 'gamma', 'gammaForward', 'vanna', 'volga', 'dvbp', 'gammacash', 'deltacash']
            let numberOfOption2 = otcOptionJson.calcInstrumentForm.moneynessOption == '1' ? calcAnnualizedByTerminationPaymentDate(otcOptionJson.calcInstrumentForm) : '1'
            arr.forEach(item=>{
                result.contract[item] = isNotVoid(result.contract[item]) ? result.contract[item] : 0;
                result.contract[item] = _.multiply( _.multiply(result.contract[item] , optionEntitlement) , numberOfOption2)
            })
            break;
      }
    return result;
}

// 根据原有的希腊字母进行扩展, Rho(1%) GammaP等
export function expandGreek(otcOptionJson, result) {
    result.contract['delta'] = isNotVoid(result.contract['delta']) ? result.contract['delta'] : 0
    result.contract['vega'] = isNotVoid(result.contract['vega']) ? result.contract['vega'] : 0
    result.contract['rho'] = isNotVoid(result.contract['rho']) ? result.contract['rho'] : 0
    result.contract['gamma'] = isNotVoid(result.contract['gamma']) ? result.contract['gamma'] : 0
    result.contract['npv'] = isNotVoid(result.contract['npv']) ? result.contract['npv'] : 0
    let optionEntitlement =  result.contract.optionEntitlement
    // Delta(手) = Delta/标的的合约乘数
    Object.assign(result.contract,  {'deltaPH':  _.divide(result.contract['delta'], otcOptionJson.calcInstrumentForm.lotSize)})
    // Vega(1%) = Vega/100
    Object.assign(result.contract, {'vegaP':  _.divide(result.contract['vega'], 100)})
    // Rho(1%) = Rho/100
    Object.assign(result.contract, {'rhoP':_.divide(result.contract['rho'], 100)})
    // GammaP = Gamma*SpotPrice/100
    Object.assign(result.contract, {'gammaP':_.divide(_.multiply(result.contract['gamma'], result.contract['spotPrice']), 100)})
    // StdDelta = Delta/本金*期初价格
    Object.assign(result.contract, {'stdDelta':_.divide(result.contract['delta'], optionEntitlement)})
    // StdGamma = Gamma/本金*期初价格
    Object.assign(result.contract, {'stdGamma':_.divide(result.contract['gamma'], optionEntitlement)})
    // 单位估值 = npv/数量 需要判断是否年化
    if (otcOptionJson.calcInstrumentForm.moneynessOption == '1') {
        Object.assign(result.contract, {'unitNpv':_.divide(_.divide(result.contract['npv'], calcAnnualizedByTerminationPaymentDate(otcOptionJson.calcInstrumentForm)), optionEntitlement)})
    } else if (otcOptionJson.calcInstrumentForm.moneynessOption == '0') {
        Object.assign(result.contract, {'unitNpv':_.divide(result.contract['npv'],  optionEntitlement)})
    }
    // 百分比价格 = npv/期初价格
    Object.assign(result.contract, {'stdNpv': _.multiply(_.divide(result.contract['unitNpv'], otcOptionJson.calcInstrumentForm.spotPrice), 100)})
}
